package cn.sailing.plugins.position.tencent;

import android.content.Context;
import android.util.Log;

import com.tencent.trackx.api.InitCallback;
import com.tencent.trackx.api.InitConfig;
import com.tencent.trackx.api.TrackerX;
import com.tencent.trackx.api.TrackerXCreator;
import com.tencent.trackx.api.model.CacheTracePoint;
import com.tencent.trackx.api.model.EntityVerifiedResult;
import com.tencent.trackx.api.model.EvictedInfo;
import com.tencent.trackx.api.model.RefluxResult;
import com.tencent.trackx.api.trace.TerminalOptions;
import com.tencent.trackx.api.trace.TraceConfig;
import com.tencent.trackx.api.trace.TraceEventCallback;
import com.tencent.trackx.api.trace.Tracer;
import com.tencent.trackx.support.nav.loc.TrackerXNavLocation;

import cn.sailing.plugins.position.IPusher;
import cn.sailing.plugins.position.Utils;

public class TencentTrackService {
    private String TENCENT_SECRET_KEY = "CUEqR17T1Qnb8WL1vU09u62NmCa3w4bL";
    private String TENCENT_APP_KEY = "262BZ-QFIKZ-DRVXU-7WLTG-TLBUE-YGFBK";
    private String Tencent_TRACK_SERVICE_ID = "671f2f6a4a9b321e155570d1";
    private TrackerX tencent_tracker;
    private Tracer tencent_tracer;
    private Context context;
    private IPusher pusher;

    private final TraceEventCallback callback = new TraceEventCallback() {
        @Override
        public void onEntityVerified(EntityVerifiedResult entityVerifiedResult) {

        }

        @Override
        public void onTraceDidStart() {

        }

        @Override
        public void onTraceDidStop() {

        }

        @Override
        public void onTracePointPreCache(CacheTracePoint cacheTracePoint) {
            float direction = cacheTracePoint.getDirection();
            double lat = cacheTracePoint.getLatitude();
            double lng = cacheTracePoint.getLongitude();
            double acc = cacheTracePoint.getAccuracy();
            float speed = cacheTracePoint.getSpeed();
            if (pusher != null) {
                pusher.Push(lng, lat, acc, speed, direction, "tencent");
            }
        }

        @Override
        public void onTracePointRefluxed(RefluxResult refluxResult) {

        }

        @Override
        public void onTracePointEvicted(EvictedInfo evictedInfo) {

        }
    };
    private boolean inited = false;

    public TencentTrackService(Context ctx, IPusher pusher) {
        this.pusher = pusher;
        context = ctx;
        tencent_tracker = TrackerXCreator.instance(context).newTrackX();
        InitConfig _config = InitConfig.newBuilder()
                .appKey(TENCENT_APP_KEY)
                .deviceId(Utils.GetDeviceId())
                .secretKey(TENCENT_SECRET_KEY)
                .debuggable(true)
                .build();
        InitCallback _callback = initResult -> {
            Log.i("初始化腾讯轨迹服务回调", initResult.getMessage());
            inited = true;
        };
        try {
            tencent_tracker.init(_config, _callback);
            tencent_tracer = tencent_tracker.tracer(
                    TerminalOptions.newBuilder(
                            Tencent_TRACK_SERVICE_ID,
                            Utils.GetDeviceName()
                    ).build());
            tencent_tracer.setTracePointSource(TrackerXNavLocation.create(context));
            TraceConfig config = TraceConfig.newBuilder().build();
            TraceConfig.newBuilder(config)
                    .traceInterval(1)
                    .packInterval(10)
                    .maxCacheCount(1000)
                    .build();
            tencent_tracer.setConfig(config);
            tencent_tracer.addTraceEventCallback(callback);
        } catch (Exception e) {
            Log.e("初始化腾讯轨迹服务", e.toString());
        }
    }

    public void Start() {
        tencent_tracer.beginTrace();
    }

    public void Stop() {
        tencent_tracer.endTrace(true);
    }

}
